<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="auto"
      v-show="showSearch"
    >
      <el-row>
        <el-form-item label="类型" prop="type">
          <el-select clearable style="width: 180px" v-model="queryParams.type">
            <el-option label="新版" value="1"></el-option>
            <el-option label="旧版" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="生产单编码" prop="produceNo">
          <el-input
            v-model.trim="queryParams.produceNo"
            style="width: 180px"
            placeholder="请输入生产单编码"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="日期" style="width: 308px">
          <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>

        <div class="operation">
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          <el-button @click="advanced = !advanced">
            <a style="color: #448ef7">
              <el-icon v-if="advanced">
                <ArrowUpBold />
              </el-icon>
              <el-icon v-else>
                <ArrowDownBold />
              </el-icon>
            </a>
          </el-button>
          <right-toolbar
            style="margin-left: 10px"
            :search="false"
            v-model:showSearch="showSearch"
            @queryTable="getList"
            :columns="columns"
          ></right-toolbar>
        </div>
      </el-row>
      <el-row v-if="advanced">
        <el-form-item label="单据编码" prop="platemakingCode">
          <el-input
            v-model.trim="queryParams.platemakingCode"
            style="width: 180px"
            placeholder="请输入制版编码"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="制版人" prop="platemakingBy">
          <el-input
            v-model.trim="queryParams.platemakingBy"
            style="width: 180px"
            placeholder="请输入制版人"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="出版编号" prop="pubNo">
          <el-input
            v-model.trim="queryParams.pubNo"
            style="width: 180px"
            placeholder="请输入出版编号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="书刊名称" prop="bookName">
          <el-input
            v-model.trim="queryParams.bookName"
            style="width: 180px"
            placeholder="请输入书刊名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="书刊编码" prop="bookNo">
          <el-input
            v-model.trim="queryParams.bookNo"
            style="width: 180px"
            placeholder="请输入书刊编码"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="版材尺寸" prop="sheetSize">
          <el-select
            clearable
            style="width: 180px"
            v-model="queryParams.sheetSize"
            placeholder="请选择版材尺寸"
          >
            <el-option
              v-for="item in ems_machine_info_size"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="品种" prop="breed">
          <el-input
            v-model.trim="queryParams.breed"
            style="width: 180px"
            placeholder="请输入品种"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="做版人" prop="makeUser">
          <el-input
            v-model.trim="queryParams.makeUser"
            style="width: 180px"
            placeholder="请输入做版人"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="车间" prop="workshopId">
          <el-select
            @change="handleQuery"
            style="width: 180px"
            v-model="queryParams.workshopId"
            placeholder="请选择"
          >
            <el-option
              v-for="item in ems_workshop"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Download" @click="handleExport"
          >导出</el-button
        >
      </el-col>
    </el-row>
    <div>
      <el-table
        v-loading="loading"
        border
        :data="dataList"
        :summary-method="getSummaries"
        show-summary
      >
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column
          label="状态"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="status"
          v-if="columns[0].visible"
        >
          <template v-slot="{ row }">
            <dict-tag :options="ems_order_status" :value="row.status" />
          </template>
        </el-table-column>
        <el-table-column
          label="单据编码"
          min-width="200"
          align="center"
          :show-overflow-tooltip="true"
          prop="platemakingCode"
          v-if="columns[3].visible"
        >
          <template v-slot="{ row }">
            <el-button type="primary" @click="handleViewCode(row)" text>{{
              row.platemakingCode
            }}</el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="车间"
          width="100"
          align="center"
          prop="workshopName"
        >
        </el-table-column>
        <el-table-column
          label="新/旧版"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="isNew"
          v-if="columns[1].visible"
        >
        </el-table-column>
        <!-- <el-table-column label="新/旧版" min-width="150" align="center" :show-overflow-tooltip="true"
                    prop="platemakingCode" v-if="columns[1].visible">
                    <template #default="scope">
                        <dict-tag :options="ems_produce_is_new" :value="scope.row.composition" />
                    </template>
                </el-table-column> -->
        <el-table-column
          label="单据日期"
          min-width="150"
          align="center"
          :show-overflow-tooltip="true"
          prop="platemakingDate"
          v-if="columns[2].visible"
        >
          <template v-slot="{ row }">
            {{ row.platemakingDate?.split("T")[0] }}
          </template>
        </el-table-column>

        <el-table-column
          label="生产单编码"
          min-width="150"
          align="center"
          :show-overflow-tooltip="true"
          prop="produceNo"
          v-if="columns[4].visible"
        >
        </el-table-column>
        <el-table-column
          label="出版编码"
          v-if="columns[5].visible"
          width="120"
          align="center"
          :show-overflow-tooltip="true"
          prop="pubNo"
        >
        </el-table-column>
        <el-table-column
          label="书刊编码"
          v-if="columns[6].visible"
          width="120"
          align="center"
          :show-overflow-tooltip="true"
          prop="bookNo"
        >
        </el-table-column>
        <el-table-column
          label="书刊名称"
          v-if="columns[7].visible"
          width="120"
          align="center"
          :show-overflow-tooltip="true"
          prop="bookName"
        >
        </el-table-column>
        <el-table-column
          label="印刷序列号"
          v-if="columns[8].visible"
          align="center"
          width="120"
          :show-overflow-tooltip="true"
          prop="printProduceNo"
        >
        </el-table-column>
        <el-table-column
          label="品种"
          v-if="columns[9].visible"
          prop="breed"
          min-width="160"
          align="center"
        >
          <!-- <template #default="scope">
                        <dict-tag :options="ems_order_breed_type" :value="scope.row.breed" />
                    </template> -->
        </el-table-column>
        <el-table-column
          label="组成"
          min-width="160"
          v-if="columns[10].visible"
          prop="composition"
          align="center"
        >
          <!-- <template #default="scope">
                        <dict-tag :options="ems_order_make_up" :value="scope.row.composition" />
                    </template> -->
        </el-table-column>
        <el-table-column
          label="印张"
          width="120"
          v-if="columns[11].visible"
          prop="sheet"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="印数"
          width="120"
          v-if="columns[12].visible"
          prop="printAmount"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="印色"
          width="120"
          v-if="columns[13].visible"
          prop="printColour"
          align="center"
        >
          <template #default="scope">
            <dict-tag
              :options="ems_order_print_color"
              :value="scope.row.printColour"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="版材数量"
          width="120"
          v-if="columns[14].visible"
          prop="sheetAmount"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="制版数量"
          width="120"
          v-if="columns[15].visible"
          prop="platemakingNumber"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="版材尺寸"
          prop="sheetSize"
          v-if="columns[16].visible"
          width="120"
          align="center"
        >
          <template #default="scope">
            <dict-tag
              :options="ems_machine_info_size"
              :value="scope.row.sheetSize"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="做版人"
          width="120"
          v-if="columns[17].visible"
          prop="makeUser"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="制版人"
          width="120"
          v-if="columns[17].visible"
          prop="platemakingBy"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="烤版人"
          width="120"
          v-if="columns[18].visible"
          prop="bakedBy"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="完成日期"
          width="120"
          v-if="columns[19].visible"
          prop="platemakingDate"
          align="center"
        >
          <template v-slot="{ row }">
            {{ row.platemakingDate.split("T")[0] }}
          </template>
        </el-table-column>
        <el-table-column
          label="取版人"
          width="120"
          v-if="columns[20].visible"
          prop="takeUser"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="取版日期"
          width="120"
          v-if="columns[12].visible"
          prop="takeDate"
          align="center"
        >
        </el-table-column>
        <el-table-column
          v-if="columns[22].visible"
          label="备注"
          align="center"
          prop="remark"
          :show-overflow-tooltip="true"
        />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script setup >
import { downLoadBlob } from "@/utils/ruoyi";
import {
  getPlatemakingDetaillist,
  exportList,
} from "@/api/production/platemaking.js";
import { ElLoading } from "element-plus";

const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const showSearch = ref(true);
const router = useRouter();
const total = ref(0);
const advanced = ref(false);
const sheetTotal = ref(0);
const platemakingTotal = ref(0);
// 列显隐信息
const columns = ref([
  { key: 0, label: `状态`, visible: true },
  { key: 1, label: `新/旧版`, visible: true },
  { key: 2, label: `制版日期`, visible: true },
  { key: 3, label: `制版编码`, visible: true },
  { key: 4, label: `生产单编码`, visible: true },
  { key: 5, label: `出版编码`, visible: true },
  { key: 6, label: `书刊编码`, visible: true },
  { key: 7, label: `书刊名称`, visible: true },
  { key: 8, label: `印刷序列号`, visible: true },
  { key: 9, label: `品种`, visible: true },
  { key: 10, label: `组成`, visible: true },
  { key: 11, label: `印张`, visible: true },
  { key: 12, label: `印数`, visible: true },
  { key: 13, label: `印色`, visible: true },
  { key: 14, label: `版材数量`, visible: true },
  { key: 15, label: `制版数量`, visible: true },
  { key: 16, label: `版材尺寸`, visible: true },
  { key: 17, label: `制版人`, visible: true },
  { key: 18, label: `烤版人`, visible: true },
  { key: 19, label: `完成日期`, visible: true },
  { key: 20, label: `取版人`, visible: true },
  { key: 21, label: `取版日期`, visible: true },
  { key: 22, label: `备注`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
});

const getDateNow = () => {
  var myDate = new Date();
  var year = myDate.getFullYear();
  var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月，所以要加1);
  var day = myDate.getDate(); //获取当前日（1-31）
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  var firstDay = year + "-" + month + "-01";
  return firstDay;
};
const getCurrentDay = () => {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1; // 月份从0开始，需要加1
  var day = now.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  const currentDay = year + "-" + month + "-" + day;
  return currentDay;
};
const dateRange = ref([getDateNow(), getCurrentDay()]);
const { queryParams } = toRefs(data);
const {
  ems_produce_is_new,
  ems_order_status,
  ems_machine_info_size,
  ems_order_make_up,
  ems_order_print_color,
  ems_order_breed_type,
  ems_workshop,
} = proxy.useDict(
  "ems_produce_is_new",
  "ems_order_status",
  "ems_order_breed_type",
  "ems_machine_info_size",
  "ems_order_make_up",
  "ems_workshop",
  "ems_order_print_color"
);

const getList = () => {
  loading.value = true;
  getPlatemakingDetaillist({
    ...queryParams.value,
    startDate: Array.isArray(dateRange.value) ? dateRange.value[0] : undefined,
    endDate:
      Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined,
  })
    .then((res) => {
      loading.value = false;
      dataList.value = res.rows;
      if (res.rows.length > 0) {
        sheetTotal.value = res.rows[0].sheetTotal;
        platemakingTotal.value = res.rows[0].platemakingTotal;
      }
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};
// 合计
const getSummaries = (param) => {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "合计";
      return;
    }
    switch (column.property) {
      case "sheetAmount":
        sums[index] = sheetTotal.value;
        break;
      case "platemakingNumber":
        sums[index] = platemakingTotal.value;
        break;
      default:
        break;
    }
  });
  return sums;
};
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = [getDateNow(), getCurrentDay()];
  proxy.resetForm("queryRef");
  handleQuery();
};
const handleExport = () => {
  const loading = ElLoading.service({
    lock: true,
    text: "正在下载",
    background: "rgba(0, 0, 0, 0.7)",
  });
  exportList({
    ...queryParams.value,
    startDate: Array.isArray(dateRange.value) ? dateRange.value[0] : undefined,
    endDate:
      Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined,
  })
    .then((res) => {
      downLoadBlob(res, "制版明细列表");
      loading.close();
    })
    .catch((err) => {
      loading.close();
    });
};

const handleViewCode = (row) => {
  router.push({
    path: "/production/platemaking/platemakingDetails",
    query: {
      id: row.platemakingId,
      info: true,
    },
  });
};

getList();
// router.beforeEach((to, from, next) => {
//     if (to.path == "/basicInfo/personnel") {
//         getList()
//     }
//     next();
// });
</script>

<style lang="scss" scoped>
.operation {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 18px;
}
</style>